/**
 * JAVACC DEMO 1.0
 */
package com.apache.rpc.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.rpc.entity.SystemConfig;
import com.apache.rpc.manager.SystemConfigManager;
import com.apache.tools.ClassToolsUtil;
import com.apache.tools.StrUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * description:  系统配置管理action
 *
 * @author Administrator 创建时间：2016-11-30
 */
public class ConfigActionPluginImpl implements PluginConnector {
    private Logger log = Logger.getLogger(getClass());
    @Autowired
    protected SystemConfigManager systemConfigManager;

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see PluginConnector#execute(ParamsVo)
     */
    public Object execute(ParamsVo param) throws Exception {
        String method = StrUtil.doNull(String.valueOf(param.getParams("method")),"copy");
        ResultEntity entity = new ResultEntity();
        log.info("method->"+method);
        entity.setResult("true");
        if ("loadCache".equalsIgnoreCase(method)) {
            loadCache(entity, param);
        } else if ("list".equalsIgnoreCase(method)) {
            list(entity, param);
        } else if ("save".equalsIgnoreCase(method)) {
            save(entity, param);
        } else if ("info".equalsIgnoreCase(method)) {
            info(entity, param);
        } else if("copy".equalsIgnoreCase(method)){
            copy(entity,param);
        }
        param = null;
        return entity;
    }

    private void copy(ResultEntity entity, ParamsVo param){
        String fileName = String.valueOf(param.getParams("fileName"));
        String newFileName = String.valueOf(param.getParams("newFileName"));
        ParamsVo voNew = new ParamsVo();
        voNew.setInfoId(newFileName);
        Object newObj = systemConfigManager.getInfoById(voNew);
        if(newObj!=null){
            SystemConfig config = (SystemConfig)newObj;
            log.info("同名文件已存在："+newFileName);
            entity.setEntity(config.getFileName());
            entity.setMessage("同名文件已存在");
        }else{
            ParamsVo vo = new ParamsVo();
            vo.setInfoId(fileName);
            Object obj = systemConfigManager.getInfoById(vo);
            if(obj != null){
                SystemConfig config = (SystemConfig)obj;
                log.info("copy old fileName->"+ config.getFileName());
                config.setFileName(newFileName);
                vo.setObj(config);
                log.info("copy new newFileName->"+ newFileName);
                String id = systemConfigManager.saveInfo(vo);
                log.info("copy new id->"+ id);
                if (Validator.isNull(id)) {
                    entity.setMessage("保存失败");
                } else {
                    entity.setEntity(id);
                    entity.setMessage("保存成功");
                }
            } else {
                entity.setMessage("缺少参数或信息不存在");
            }
        }
    }

    private void info(ResultEntity entity, ParamsVo param) {
        String fileName = String.valueOf(param.getParams("fileName"));
        ParamsVo vo = new ParamsVo();
        vo.setInfoId(fileName);
        Object obj = systemConfigManager.getInfoById(vo);
        entity.setEntity(obj);
        entity.setMessage("查询成功");
    }

    private void loadCache(ResultEntity entity, ParamsVo param) {
        ParamsVo vo = new ParamsVo();
        SystemConfig config = new SystemConfig();
        vo.setObj(config);
        vo.setMethodKey("initCache");
        systemConfigManager.execute(vo);
        entity.setEntity("true");
        entity.setMessage("操作成功！");
    }

    private void list(ResultEntity entity, ParamsVo param) {
        ParamsVo<SystemConfig> vo = new ParamsVo<SystemConfig>();
        SystemConfig rds = new SystemConfig();
        vo.setObj(rds);
        String rows = StrUtil.doNull(String.valueOf(param.getParams("pageSize")),
                String.valueOf(param.getParams("rows")));
        String page = StrUtil.doNull(String.valueOf(param.getParams("pageIndex")),
                String.valueOf(param.getParams("page")));
        vo.setParams("pageIndex", page);
        vo.setParams("pageSize", rows);
        Page pageList = systemConfigManager.getPageInfo(vo);
        entity.setEntity(pageList);
        entity.setMessage("查询成功");
    }

    private void save(ResultEntity entity, ParamsVo vo) {
        ParamsVo<SystemConfig> param = new ParamsVo<SystemConfig>();
        SystemConfig item = new SystemConfig();
        item = (SystemConfig) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), item);
        if (Validator.isNull(item.getFileName())) {
            entity.setMessage("数据源名称不能空");
            return;
        } else {
            param.setObj(item);
            param.setInfoId(item.getFileName());
            Object obj = systemConfigManager.getInfoById(param);
            if (null == obj) {
                String id = systemConfigManager.saveInfo(param);
                if (Validator.isNull(id)) {
                    entity.setMessage("保存失败");
                } else {
                    entity.setEntity(id);
                    entity.setMessage("保存成功");
                }
            } else {
                boolean mark = systemConfigManager.editInfo(param);
                if (mark) {
                    entity.setEntity(mark);
                    entity.setMessage("修改成功");
                } else {
                    entity.setMessage("修改失败");
                }
            }
        }
    }

    public void setSystemConfigManager(SystemConfigManager systemConfigManager) {
        this.systemConfigManager = systemConfigManager;
    }
}
